/*
 * @Description: i18n实例
 * @Author: HuiSir<273250950@qq.com>
 * @Date: 2022-04-19 10:00:02
 * @LastEditTime: 2025-08-12 11:30:48
 */
import { App } from "vue";
import { createI18n } from "vue-i18n";
import LocalCache from "@/utils/LocalCache";
import { project } from "@/init/config";
import zh from "./locales/zh.json";
import en from "./locales/en.json";

// 获取缓存语言
const langCache = LocalCache.getCache(project.storage.lang);
// 根据浏览器navigator判断当前浏览器语言
const navigatorLang = window.navigator.language.includes("zh") ? "zh" : "en";

/**
 * 创建i18n实例
 * 当前暂定中英两种语言
 */
export default (app: App<string>) => {
  const i18n = createI18n({
    locale: langCache || navigatorLang,
    messages: {
      zh,
      en,
    },
  });

  app.config.globalProperties.$t = i18n.global.t;
  app.config.globalProperties.$setLocale = (lang: "zh" | "en") => {
    i18n.global.locale = lang;
    LocalCache.setCache(project.storage.lang, lang);
  };
  app.config.globalProperties.$getLocale = () => i18n.global.locale;

  return app.use(i18n);
};
